package cn.zwxky.android.staff.My;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbFragment;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

import cn.zwxky.android.staff.Jurisdiction.Marketplace.MarketplaceManageActivity;
import cn.zwxky.android.staff.Jurisdiction.OtherSectors.OtherOectorsManageActivity;
import cn.zwxky.android.staff.Login.LoginActivity;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.DataCleanManager;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.Permission;
import cn.zwxky.android.staff.Utils.PopWindowManager;
import cn.zwxky.android.staff.Utils.URLString;


/**
 * Created by Administrator on 2017/3/15 0015.
 */

public class MyFragment extends LsfbFragment {


    /**
     * view 对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    @ViewInject(R.id.tv_badg)
    private TextView tv_badg;

    /**
     * 昵称
     */
    @ViewInject(R.id.fg_text_nikename)
    private TextView text_nikename;

    @ViewInject(R.id.my_news_remind)
    private ImageView my_news_remind;

    /**
     * 缓存大小
     */
    @ViewInject(R.id.aty_setting_text_number)
    private TextView text_number;

    @ViewInject(R.id.fg_my_img_head)
    private SimpleDraweeView img_head;

    @ViewInject(R.id.fg_my_manage)
    private RelativeLayout fg_my_manage;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    /**
     * 用于更新主线程UI
     */
    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
//            getCache();
            SN.SnackShow(text_number, "清理缓存完成");
            text_number.setText("0 KB");
        }
    };


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, null);
        ViewInjectUtils.inject(this, view);
        LsfbEvent.getInstantiation().register2(this);
        Permission permission = new Permission(mactivity);
        permission.GetMeshPermission();
        permission.GetAddressBookPermission();
        permission.GetLocationPermission2();


        if (LittleUtils.nodel.equals("0")) {
            if (LittleUtils.depart.equals("1") || LittleUtils.depart.equals("2") || LittleUtils.depart.equals("3")) {
                if (LittleUtils.job_yw.equals("1")) {
                    //市场部与总经理除了市场部讨论页面不一致，其余页面一样
                    fg_my_manage.setVisibility(View.VISIBLE);
                } else {
                    fg_my_manage.setVisibility(View.GONE);
                }
            } else {
                if (LittleUtils.job_yw.equals("1")) {
                    //其他部门
                    fg_my_manage.setVisibility(View.VISIBLE);
                } else {
                    fg_my_manage.setVisibility(View.GONE);
                }
            }
        } else {
            //市场部与总经理除了市场部讨论页面不一致，其余页面一样
            fg_my_manage.setVisibility(View.VISIBLE);
        }


        getCache();
        getData();
        getNumber();
        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.fg_my_manage, R.id.fg_msg, R.id.fg_my_details, R.id.fg_pwd, R.id.fg_saddress_book, R.id.fg_system_msg, R.id.aty_clean, R.id.fg_exit})
    public void onClick(View view) {
        switch (view.getId()) {
            //公司公告
            case R.id.fg_msg:
                startActivity(new Intent(mactivity, CompanyMsgActivity.class));
                break;
            //权限管理
            case R.id.fg_my_manage:
                if (LittleUtils.nodel.equals("0")) {
                    if (LittleUtils.depart.equals("1") || LittleUtils.depart.equals("2") || LittleUtils.depart.equals("3")|| LittleUtils.depart.equals("5")) {
                        if (LittleUtils.job_yw.equals("1")) {
                            //市场部与总经理除了市场部讨论页面不一致，其余页面一样
                            if (LittleUtils.depart_yw.equals("1")) {
                                startActivity(new Intent(mactivity, MarketplaceManageActivity.class));
                            } else {
                                startActivity(new Intent(mactivity, OtherOectorsManageActivity.class));
                            }
                        } else {
                            SN.SnackShow(img_head, "你暂时没有权限进入");
                        }
                    } else {
                        if (LittleUtils.job_yw.equals("1")) {
                            //其他部门
                            startActivity(new Intent(mactivity, OtherOectorsManageActivity.class));
                        } else {
                            SN.SnackShow(img_head, "你暂时没有权限进入");
                        }
                    }
                } else {
                    //市场部与总经理除了市场部讨论页面不一致，其余页面一样
                    startActivity(new Intent(mactivity, MarketplaceManageActivity.class));
                }
                break;
            //资料管理
            case R.id.fg_my_details:
                startActivity(new Intent(mactivity, MyDetailsActivity.class));
                break;
            //密码管理
            case R.id.fg_pwd:
                startActivity(new Intent(mactivity, PwdActivity.class));
                break;
            //通讯录
            case R.id.fg_saddress_book:
                startActivity(new Intent(mactivity, SaddressBookActivity.class));
                break;
            //系统消息
            case R.id.fg_system_msg:
                startActivity(new Intent(mactivity, SystemMsgActivity.class));
                break;
            //清除缓存
            case R.id.aty_clean:
                DataCleanManager.cleanApplicationData(mactivity.getApplicationContext(), "");
                handler.sendEmptyMessageDelayed(1000, 2000);
                T.showShort(mactivity, "缓存清理中...");

                break;
            //退出
            case R.id.fg_exit:
                replay = new PopWindowManager(mactivity, R.layout.pop_exit) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final TextView pop_exit_yes = (TextView) contentView.findViewById(R.id.pop_exit_yes);
                        final TextView pop_exit_no = (TextView) contentView.findViewById(R.id.pop_exit_no);
                        final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);

                        //退出
                        pop_exit_yes.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                LittleUtils.exit(mactivity);
                                startActivity(new Intent(mactivity, LoginActivity.class));
                            }
                        });

                        pop_exit_no.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                manager.hideWindow();
                            }
                        });

                        RelativeLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                manager.hideWindow();
                            }
                        });

                    }
                };
                replay.showWindowsCenter(mactivity.getWindow().getDecorView());
                break;
        }
    }


    @Override
    protected void Resume() {
        getData();
        getNumber();
    }

    @Override
    public void initData() {

    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.INFO, hashMap, MyDetailsBean.class, new MyDetailsEvent<MyDetailsBean>(), true, true);
    }

    @EventAnnotation
    public void MyDetailsEvent(MyDetailsEvent<MyDetailsBean> event) {
        switch (event.getCode()) {
            case 200:
                if (!event.getData().getImg().isEmpty()) {
                    img_head.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getImg()));
                } else {
                    img_head.setBackgroundResource(R.mipmap.img_my_head);
                }
                text_nikename.setText(event.getData().getName());

                if (event.getData().getL_num().equals("0") && event.getData().getS_num().equals("0")) {
                    my_news_remind.setVisibility(View.GONE);
                } else {
                    my_news_remind.setVisibility(View.VISIBLE);
                }

                break;
            default:
                break;
        }
    }

    /**
     * 获取公司公告新闻条数
     */
    public void getNumber() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.NEWSCOUNT, hashMap, NewsCountBean.class, new NewsCountEvent<NewsCountBean>(), true, true);
    }

    @EventAnnotation
    public void NewsCountEvent(NewsCountEvent<NewsCountBean> event) {
        switch (event.getCode()) {
            case 200:
                if (event.getData().getCount().equals("0")) {
                    tv_badg.setVisibility(View.GONE);
                } else {
                    tv_badg.setVisibility(View.VISIBLE);
                    tv_badg.setText(event.getData().getCount());
                }

                break;
            default:
                break;
        }
    }


    /**
     * 获取缓存数据
     */
    public void getCache() {
        try {
            text_number.setText(DataCleanManager.getTotalCacheSize(mactivity.getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onDestroy() {
        LsfbEvent.getInstantiation().unregister2(this);
        super.onDestroy();
    }

}
